package com.leostadyn.stackfarm.timestatistics.model.mentity.admin;

import android.os.Handler;

import com.leostadyn.stackfarm.timestatistics.model.bean.AddProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.DeleteBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.ProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UpdateInfoBean;
import com.leostadyn.stackfarm.timestatistics.model.minterface.admin.IProjectManageModel;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;


public class ProjectManageModel {

    public static void refresh(HashMap<String, String> map, CompositeDisposable compositeDisposable, final IProjectManageModel callback) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ArrayList<ProjectBean> projectList = new ArrayList<>();
                AdminApi.getInstance().getProjectList(map).subscribe(new OriginalSubscriber<List<ProjectBean>>(compositeDisposable) {
                    @Override
                    public void onError(ResponseThrowable e) {
                        callback.onFailure();
                    }

                    @Override
                    public void onNext(List<ProjectBean> projectBeans, String msg) {
                        callback.successProject(projectList);
                        callback.onCompleted();
                    }
                });
            }
        }, 100);

    }

    //删除
    public static void delete(HashMap<String, String> map, CompositeDisposable compositeDisposable, final IProjectManageModel callback) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AdminApi.getInstance().deleteData(map).subscribe(new OriginalSubscriber<DeleteBean>(compositeDisposable) {
                    @Override
                    public void onError(ResponseThrowable e) {
                        //ToastUtil.show(StaffListActivity.this, e.getMsg());
                        callback.onFailure();
                    }

                    @Override
                    public void onNext(DeleteBean deleteBean, String msg) {
                        callback.onSuccess();
                    }
                });
            }
        }, 100);
    }

    public static void addProject(HashMap<String, String> map, CompositeDisposable compositeDisposable, final IProjectManageModel callback) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AdminApi.getInstance().addProject(map).subscribe(new OriginalSubscriber<AddProjectBean>(compositeDisposable) {
                    @Override
                    public void onError(ResponseThrowable e) {
                        //ToastUtil.show(StaffListActivity.this, e.getMsg());
                        callback.onFailure();
                    }

                    @Override
                    public void onNext(AddProjectBean addProjectBean, String msg) {
                        callback.onSuccess();

                    }

                });
            }
        }, 100);
    }

    public static void updateProject(HashMap<String, String> map, CompositeDisposable compositeDisposable, final IProjectManageModel callback) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AdminApi.getInstance().updateProjectInfo(map).subscribe(new OriginalSubscriber<UpdateInfoBean>(compositeDisposable) {
                    @Override
                    public void onError(ResponseThrowable e) {
                        //ToastUtil.show(StaffListActivity.this, e.getMsg());
                        callback.onFailure();
                    }

                    @Override
                    public void onNext(UpdateInfoBean updateProjectBean, String msg) {
                        callback.onSuccess();
                    }

                });
            }
        }, 100);
    }

}
